package com.woniu.elderly.operation.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.protocol.x.Notice;
import com.woniu.common.entity.Opeemployee;
import com.woniu.common.entity.Openotice;
import com.woniu.common.entity.PageBean;

import com.woniu.common.entity.ServePerson;
import com.woniu.elderly.operation.mapper.OpeemployeeMapper;
import com.woniu.elderly.operation.mapper.OpenoticeMapper;
import com.woniu.elderly.operation.service.OpenoticeService;
import com.woniu.elderly.operation.util.SerialNumberGenerator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class OpenoticeServiceImpl extends ServiceImpl<OpenoticeMapper, Openotice>
        implements OpenoticeService {
    @Autowired
    OpenoticeMapper openoticeMapper;
    @Autowired
    OpeemployeeMapper opeemployeeMapper;


    @Override
    public PageBean<Openotice> getNoticeList(int pageIndex, int pageSize, Openotice openotice) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Openotice> openotices=openoticeMapper.selectNoticeList(openotice);
        //pageInfo封装相关的所有数据
        PageInfo<Openotice> pageInfo=new PageInfo<>(openotices);
        PageBean<Openotice> pageBean=new PageBean<>();
        pageBean.setPageIndex(pageIndex);//第几页
        pageBean.setPageSize(pageSize);//每页显示的条数
        pageBean.setTotalPage(pageInfo.getPages());//总页数
        pageBean.setTotalRecord((int)pageInfo.getTotal());//数据总条数
        pageBean.setBeanList(pageInfo.getList());//当前页的数据集合
        return pageBean;
    }

    @Override
    public void deleteByNid(long nid) {
        openoticeMapper.deleteById(nid);
    }

    public void addNotice(Openotice openotice/*, MultipartFile file*/){
        /*List<Openotice> openotices = baseMapper.selectList(
                Wrappers
                        .<Openotice>lambdaQuery()
                        .select(Openotice::getNoticenum)
                        .last("or isdelete in (0,1)")
        );

        //获取最大工号
        int max = 0;
        for(int i = 1; i < openotices.size(); i++){
            max = Integer.parseInt(openotices.get(0).getNoticenum());
            if(Integer.parseInt(openotices.get(i).getNoticenum()) > max){
                max = Integer.parseInt(openotices.get(i).getNoticenum());
            }
        }
        max ++;

        //将int类型转换成string类型，不够位补0
        String jobNumber = new DecimalFormat("0000").format(max);
        String number = SerialNumberGenerator.generateorderNumber();
        //返回给前端一个提示信息
        String str = number + jobNumber ;
        openotice.setNoticenum(str);*/

        /*try {
            // 生成图片的唯一路径
            String uniquePath = UUID.randomUUID().toString() + "_" + file.getOriginalFilename();
            // 假设你有一个目录 /uploads/images/ 来存储图片
            String finalPath = "D:/java/images/" + uniquePath;
            // 保存文件到服务器
            file.transferTo(new File(finalPath));
            // 将图片路径保存到数据库
            openotice.setNcover(finalPath);
            // 这里假设你有一个saveOrUpdate方法来保存或更新Notice
            openoticeMapper.insertNotice(openotice); // 或者使用MyBatis Plus的saveOrUpdate方法，如果你扩展了BaseMapper
            //return "文件上传成功，路径为：" + finalPath;
        } catch (Exception e) {
            throw new RuntimeException("文件上传失败：" + e.getMessage());
        }*/

        openoticeMapper.insertNotice(openotice);
    }

    @Override
    public void sendNotice(Openotice openotice, List<Opeemployee> opeemployees) {
        // 保存通知
        openoticeMapper.insert(openotice);
        // 设置接收人的通知ID和保存接收人信息
        opeemployees.forEach(opeemployee -> {
            opeemployee.setNid(openotice.getNid());
            opeemployeeMapper.insert(opeemployee);
        });
    }

    @Override
    public void updateNotice(Openotice openotice) {
        openoticeMapper.updateNotice(openotice);
    }

    @Override
    public Openotice getNoticeByNid(long nid) {
        return openoticeMapper.selectById(nid);
    }

    @Override
    public Openotice getContentByNid(long nid) {
        return openoticeMapper.selectContentByNid(nid);
    }

    @Override
    public PageBean<Openotice> getNoticeListByNstate(int pageIndex, int pageSize, Openotice openotice) {
        PageHelper.startPage(pageIndex,pageSize);
        List<Openotice> openotices=openoticeMapper.selectNoticeListByNstate(openotice);
        //pageInfo封装相关的所有数据
        PageInfo<Openotice> pageInfo=new PageInfo<>(openotices);
        PageBean<Openotice> pageBean=new PageBean<>();
        pageBean.setPageIndex(pageIndex);//第几页
        pageBean.setPageSize(pageSize);//每页显示的条数
        pageBean.setTotalPage(pageInfo.getPages());//总页数
        pageBean.setTotalRecord((int)pageInfo.getTotal());//数据总条数
        pageBean.setBeanList(pageInfo.getList());//当前页的数据集合
        return pageBean;
    }

    @Override
    public int addViewCountById(long nid) {
        return openoticeMapper.increaseViewCountById(nid);
    }
}
